//
//  NdUncaughtExceptionHandler.m
//  BigToBig
//
//  Created by admin on 16/6/7.
//  Copyright © 2016年 cn.cloudtp. All rights reserved.
//

#import "NdUncaughtExceptionHandler.h"

//@implementation NdUncaughtExceptionHandler


@implementation NdUncaughtExceptionHandler

-(NSString *)applicationDocumentsDirectory {
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

+ (void)setDefaultHandler
{
    NSSetUncaughtExceptionHandler (&UncaughtExceptionHandler);
}

+ (NSUncaughtExceptionHandler*)getHandler
{
    return NSGetUncaughtExceptionHandler();
}

NSString *applicationDocumentsDirectory() {
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

//void UncaughtExceptionHandler(NSException *exception) {
//    NSArray *arr = [exception callStackSymbols];
//    NSString *reason = [exception reason];
//    NSString *name = [exception name];
//
//    NSString *url = [NSString stringWithFormat:@"=============异常崩溃报告=============\nname:\n%@\nreason:\n%@\ncallStackSymbols:\n%@",
//                     name,reason,[arr componentsJoinedByString:@"\n"]];
//    NSString *path = [applicationDocumentsDirectory() stringByAppendingPathComponent:@"Exception.txt"];
//    [url writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
//    //除了可以选择写到应用下的某个文件，通过后续处理将信息发送到服务器等
//    //还可以选择调用发送邮件的的程序，发送信息到指定的邮件地址
//    //或者调用某个处理程序来处理这个信息
//}

void UncaughtExceptionHandler(NSException *exception) {
    
    
    NSUserDefaults *zhiBoUser = [NSUserDefaults standardUserDefaults];
    NSString *zhiBoId = [zhiBoUser objectForKey:@"zhiboID"];
    NSLog(@"%@",zhiBoId);
    
    NSDate *currentDate = [NSDate date];//获取当前时间，日期
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    
    NSString *dateString = [dateFormatter stringFromDate:currentDate];
    
    
    NSString *str = [NSString stringWithFormat:@"http://61.135.176.43:6080/bigtobig/live/update.json?id=%@&end_time=%@",zhiBoId,dateString];
    
    NSString *urlstr = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSURL *urlstr1 = [NSURL URLWithString:urlstr];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:urlstr1];
    
    [request setValue:@"application/json,text/json,text/javascript,text/html" forHTTPHeaderField:@"Content-Type"];
    NSLog(@"%@",urlstr);
    // 4.发送请求
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        NSString *str = dict[@"return_code"];
        NSLog(@"%@",str);
        NSLog(@"%@",dict);
        
        
    }];
//    NSArray *arr = [exception callStackSymbols];
//    NSString *reason = [exception reason];
//    NSString *name = [exception name];
//
//    NSString *url = [NSString stringWithFormat:@"=============异常崩溃报告=============\nname:\n%@\nreason:\n%@\ncallStackSymbols:\n%@",
//                     name,reason,[arr componentsJoinedByString:@"\n"]];
//    NSString *path = [applicationDocumentsDirectory() stringByAppendingPathComponent:@"Exception.txt"];
//    [url writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
    //除了可以选择写到应用下的某个文件，通过后续处理将信息发送到服务器等
    //还可以选择调用发送邮件的的程序，发送信息到指定的邮件地址
    //或者调用某个处理程序来处理这个信息
}

@end
